var express = require('express');
var path = require('path');
var port = process.env.PORT||5000;
var favicon = require('static-favicon');
var bodyParser = require('body-parser');
var cookieParser = require('cookie-parser');
var mongoose = require('mongoose');
var app = express();
console.log('Service will start on port: '+port);

mongoose.connect('mongodb://localhost/modle');
var love = require('./models/love');
var hate = require('./models/hate');

app.set('views','./views/pages');
app.set('view engine','jade');
app.listen(port);
console.log('Service has been started on port:'+port);
app.use(favicon());
app.use(bodyParser.urlencoded());
app.use(bodyParser.json());
app.use(cookieParser());
app.use(express.static(path.join(__dirname,'public')));
console.log('Every part is ready for Run!');

//后台向数据库添加数据处
	//love collections
//var ooo = new love({
//	title:'关雎',
//	poet:'诗经',
//	content:'关关雎鸠，在河之洲，窈窕淑女，君子好逑，参差荇菜，左右流之',
//	simple:'窈窕淑女,君子好逑...'
//})
// ooo.save(function(err){
// 	if(err){console.log(err)}
// 	else{console.log('success')}
// })
	//hate collections
//var aaa = new hate({
//	title:'精忠报国',
//	poet:'屠洪刚',
//	content:'狼烟起，江山北望，风起卷马长嘶剑气如霜，心似黄河水茫茫，忍叹息更无语血泪满行',
//	simple:'堂堂中华要让四方，来贺'
//})
//aaa.save(function(err,doc){
//	if(err){console.log(err)}
//	else{console.log('sucess to save hate')}
//})

//get区域*****************************************
app.get('/',function(req,res){
	res.render('index',{
		maintitle:'阴晴书楼'
	})
})
//love显示数据
app.get('/love',function(req,res){
	love.find({},function(err,doc){
		if(err){
			console.log(err);
		}else
		{
			console.log(doc);
			res.render('love',{
				maintitle:'晴的红润',
				loves:doc
			})
		}
	})
})
//showlove显示数据
app.get('/love/:title',function(req,res){
	var title = req.params.title;
	console.log('得到的url参数是：'+title);
	love.find({title:title},function(err,doc){
		if(err){
			console.log(err);
		}
		else{
			console.log('查找成功准备显示');
			res.render('showlove',{
				maintitle:title,
				love:doc
			})
		}
	})
})
//hate显示数据
app.get('/hate',function(req,res){
	hate.find({},function(err,doc){
		if(err){
			console.log(err);
		}else{
			res.render('hate',{
				maintitle:'阴的理性',
				detail:doc
			})
		}
	})
})
app.get('/add',function(req,res){
	res.render('add',{
		maintitle:'新题'
	})
})
app.get('/success',function(req,res){
	res.render('success',{
		maintitle:'成功提示'
	})
})
app.get('/update/:title',function(req,res){
	var title = req.params.title;
	love.find({title:title},function(err,doc){
		if(err){
			console.log(err);
		}else{
			res.render('update',{
				maintitle:title+'修改页面',
				updatelove:doc
			})
		}
	})
})
app.get('/all',function(req,res){
	res.render('all',{
		maintitle:'阴&晴'
	})
})
app.get('/showhate/:title',function(req,res){
	var title = req.params.title;
	hate.find({title:title},function(err,doc){
		if(err){
			console.log(err);
		}else{
			res.render('showhate',{
				maintitle:title,
				hate:doc
			})
		}
	})
})
app.get('/updatehate/:title',function(req,res){
	var title = req.params.title;
	hate.find({title:title},function(err,doc){
		if(err){
			console.log(err);
		}else{
			res.render('updatehate',{
				maintitle:title+'修改',
				updatehate:doc
			})
		}
	})
})
//post区域********************************************
	//添加数据
		//要分清楚情况，选哪个按钮存入哪个数据库
			//这里暂时是不对的
app.post('/add',function(req,res){
	var title = req.body.title;
	var poet = req.body.poet;
	var simple = req.body.simple;
	var content = req.body.content;
	var choice = req.body.choice;
	console.log('选择的数据库是:'+choice);
	var query = {title:title,poet:poet,simple:simple,content:content};
	//判断是哪个数据库从而进行操作
	if(choice=='sun'){
		console.log('现在已经选中了sun-love数据库');
		love.count(query,function(err,doc){
			if(err){
				console.log(err);
			}else{
				if(doc==0){
					var newlove = new love({
						title:title,
						poet:poet,
						simple:simple,
						content:content
					});
					newlove.save(function(err,doc){
						if(err){
							console.log(err);
						}else{
							console.log('成功存储到数据库love');
							res.redirect('/love');
						}
					})
				}
			}
		})
	}
	else
	{
		console.log('将要存储到数据库hate');
		hate.count(query,function(err,doc){
			if(err){
				console.log(err);
			}else{
				if(doc==0){
					var newhate = new hate({
						title:title,
						poet:poet,
						content:content,
						simple:simple
					});
					newhate.save(function(err,doc){
						if(err){
							console.log(err);
						}else{
							console.log('存储进hate的数据是：'+doc);
							res.redirect('/hate');
						}
					})
				}
			}
		})
	}
});
//删除操作
app.post('/showlove/:title',function(req,res){
	var title = req.params.title;
	console.log('要删除的数据是:'+title);
	love.remove({title:title},function(err){
		if(err){
			console.log(err);
		}
		else{
			console.log('删除成功！');
			res.redirect('/love');
		}
	})
})
app.post('/showhate/:title',function(req,res){
	var title = req.params.title;
	hate.remove({title:title},function(err){
		if(err){
			console.log(err);
		}else{
			console.log('hate delete successfully!');
			res.redirect('/hate');
		}
	})
})
//修改操作
app.post('/update/:title',function(req,res){
	var poet = req.body.updatepoet;
	var simple = req.body.updatesimple;
	var content = req.body.updatecontent;
	var title = req.params.title;
	console.log('接受到的消息！！！！：'+poet+' '+simple+' '+content+' '+title);
	love.update({title:title},{poet:poet,simple:simple,content:content},function(err){
		if(err){
			console.log(err);
		}else{
			console.log('update successfully!!!');
			res.redirect('/love');
		}
	})
})
app.post('/updatehate/:title',function(req,res){
	var poet = req.body.updatehatepoet;
	var simple = req.body.updatehatesimple;
	var content = req.body.updatehatecontent;
	var title = req.params.title;
	console.log('接受到的消息！！！！：'+poet+' '+simple+' '+content+' '+title);
	hate.update({title:title},{poet:poet,simple:simple,content:content},function(err){
		if(err){
			console.log(err);
		}else{
			console.log('ok!');
			res.redirect('/hate');
		}
	})
})
